package com.gitee.huanminabc.fastboot.web.servlet;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONWriter;
import com.gitee.huanminabc.fastboot.web.context.ApiThreadContext;
import com.gitee.huanminabc.jnetty.utils.NettyHttpUtil;
import com.gitee.huanminabc.nullchain.common.NullResult;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;

import java.io.File;
import java.io.InputStream;
import java.util.Map;

/**
 * 简要描述
 *
 * @Author: huanmin
 * @Date: 2025/4/2 00:14
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
public class HttpResponseUtil {

    public static void sendClient(NullResult<?> result) {
        ApiThreadContext apiThreadContext = ApiThreadContext.get();
        ChannelHandlerContext ctx = apiThreadContext.getCtx();
        //判断是否已经关闭了,表示请求提前结束了,单独被处理了
        if (!ctx.channel().isActive()) {
            return;
        }
        Map<String, Object> respHeader = apiThreadContext.getRespHeader();
        Object data = result.getData();
        if (data == null) {
            NettyHttpUtil.Response(ctx, NettyHttpUtil.JSON_CONTENT_TYPE_UTF_8, Unpooled.copiedBuffer(JSONObject.toJSONString(result), CharsetUtil.UTF_8), respHeader);
            return;
        }
        Class<?> aClass = data.getClass();
        //处理返回byte[]
        if (aClass == byte[].class) {
            byte[] bytes = (byte[]) data;
            NettyHttpUtil.responseByte(bytes, apiThreadContext.getRespDownFileName(), ctx, respHeader);
            return;
        }
        //处理返回File
        if (aClass == File.class) {
            File file = (File) data;
            NettyHttpUtil.responseFile(file, ctx, respHeader);
            return;
        }
        //处理返回InputStream
        if (InputStream.class.isAssignableFrom(aClass)) {
            java.io.InputStream inputStream = (java.io.InputStream) data;
            NettyHttpUtil.responseInputStream(inputStream,apiThreadContext.getRespDownFileName(),  ctx, respHeader);
            return;
        }
        NettyHttpUtil.Response(ctx, NettyHttpUtil.JSON_CONTENT_TYPE_UTF_8, Unpooled.copiedBuffer(JSONObject.toJSONString(result), CharsetUtil.UTF_8), respHeader);
    }

    //解决绕过返回参数是NullResult的规范问题, 不要json响应格式
    public static NullResult<Void> sendClient(String content) {
        return sendClient(Unpooled.copiedBuffer(content, CharsetUtil.UTF_8),NettyHttpUtil.TEXT_CONTENT_TYPE);
    }

    public static NullResult<Void> sendClient(ByteBuf buf, String contentType) {
        if (buf == null||contentType == null) {
            throw  new IllegalArgumentException("ByteBuf and contentType must not be null");
        }
        ApiThreadContext apiThreadContext = ApiThreadContext.get();
        ChannelHandlerContext ctx = apiThreadContext.getCtx();
        //判断是否已经关闭了,表示请求提前结束了,单独被处理了
        if (!ctx.channel().isActive()) {
            return NullResult.success();
        }
        Map<String, Object> respHeader = apiThreadContext.getRespHeader();
        NettyHttpUtil.Response(ctx, contentType, buf, respHeader);
        return NullResult.success();
    }
}
